/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package restinterpreter;

import java.awt.Color;

import javax.swing.JOptionPane;
import javax.swing.ScrollPaneConstants;


/**
 *
 * @author pvcustomer
 */
public class RestInterpreterUI extends javax.swing.JFrame {

    /**
     * Creates new form RestInterpreterUI
     */
    public RestInterpreterUI() {
    	initComponents();
    	jTabbedPane1.setMaximumSize(jTabbedPane1.getSize());
    	jPanel1.setBackground(Color.WHITE);
    	this.getContentPane().setBackground(Color.WHITE); 
    	this.repaint();
    }

    /**
     * This method is called from within the constructor to initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is always
     * regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">                          
    private void initComponents() {

        exit_Button = new javax.swing.JButton();
        raw_data_output = new javax.swing.JTextArea();
        scroll_pane = new javax.swing.JScrollPane(raw_data_output, ScrollPaneConstants.VERTICAL_SCROLLBAR_ALWAYS, ScrollPaneConstants.HORIZONTAL_SCROLLBAR_NEVER);
        jPanel1 = new javax.swing.JPanel();
        url_Textfield = new javax.swing.JTextField("http://");
        url_Label = new javax.swing.JLabel();
        send_Button = new javax.swing.JButton();
        jTabbedPane1 = new javax.swing.JTabbedPane();
        chart = new Chart_Test();
        channel1 = new ChannelREST();

        setDefaultCloseOperation(javax.swing.WindowConstants.EXIT_ON_CLOSE);
        setTitle("RestInterpreter");
       
        url_Textfield.selectAll();
   

        exit_Button.setText("Exit");
        exit_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                exit_ButtonActionPerformed(evt);
            }
        });

        jPanel1.setBorder(javax.swing.BorderFactory.createTitledBorder(null, "RestInterpreter", javax.swing.border.TitledBorder.DEFAULT_JUSTIFICATION, javax.swing.border.TitledBorder.DEFAULT_POSITION, null, new java.awt.Color(0, 51, 255)));
        jPanel1.setToolTipText("");
        

        url_Label.setText("URL:");

        send_Button.setText("GET");
        send_Button.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                send_ButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanel2Layout = new javax.swing.GroupLayout(chart.get_ChartPanel());
        chart.get_ChartPanel().setLayout(jPanel2Layout);
        jPanel2Layout.setHorizontalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 561, Short.MAX_VALUE)
        );
        jPanel2Layout.setVerticalGroup(
            jPanel2Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGap(0, 282, Short.MAX_VALUE)
        );
        
       
        
        jTabbedPane1.addTab("Enrgy Meter Chart", chart.get_ChartPanel());
       
        jTabbedPane1.addTab("Raw Data", scroll_pane);
        raw_data_output.setLineWrap(true);
        raw_data_output.setAutoscrolls(true);
        
       
       
 
        
        
        javax.swing.GroupLayout jPanel1Layout = new javax.swing.GroupLayout(jPanel1);
        jPanel1.setLayout(jPanel1Layout);
        jPanel1Layout.setHorizontalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addComponent(url_Label)
                        .addGap(18, 18, 18)
                        .addComponent(url_Textfield, javax.swing.GroupLayout.PREFERRED_SIZE, 529, javax.swing.GroupLayout.PREFERRED_SIZE))
                    .addComponent(jTabbedPane1, javax.swing.GroupLayout.PREFERRED_SIZE, 566, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(send_Button)
                .addGap(21, 21, 21))
        );
        jPanel1Layout.setVerticalGroup(
            jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanel1Layout.createSequentialGroup()
                .addGap(21, 21, 21)
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(url_Label)
                    .addComponent(url_Textfield, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanel1Layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addGap(435, 435, 435)
                        .addComponent(send_Button)
                        .addGap(0, 0, Short.MAX_VALUE))
                    .addGroup(jPanel1Layout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTabbedPane1)))
                .addContainerGap())
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                .addComponent(exit_Button)
                .addGap(26, 26, 26))
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                .addGap(33, 33, 33)
                .addComponent(jPanel1, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addComponent(exit_Button)
                .addContainerGap(javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE))
        );
        
       
        pack();
    }// </editor-fold>                        

    private void exit_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
        System.exit(0);
    }                                           

    private void send_ButtonActionPerformed(java.awt.event.ActionEvent evt) {                                            
    	if(url_Textfield.getText().isEmpty())
    	{
    		JOptionPane message = new JOptionPane();
    		JOptionPane.showMessageDialog(url_Textfield, "No Url. Please insert a URL!");
    	}
    	else if( url_Textfield.getText().equals("http://"))
    	{
    		JOptionPane message = new JOptionPane();
    		JOptionPane.showMessageDialog(url_Textfield, "No Url. Please insert a URL!");
    	}
    	else
    	{
    		if(channel1.setUrlString(url_Textfield.getText()))
    		{
    			channel1.queryUrl();
    			channel1.parseHttpReply();
    			chart.set_data_length(channel1.getTsLength());
        		raw_data_output.insert(channel1.queryUrl()+"\n\n"+channel1.getHttpReply(),0);
        		init_chart_data();
        		scroll_pane.repaint();	
    		}
    		else
    		{
    			JOptionPane message = new JOptionPane();
        		JOptionPane.showMessageDialog(url_Textfield, "No Url. Please insert a URL!");
    		}
    	}
    }                                                                                   
    
    private void init_chart_data()
    {
    	for(int i=0;i<channel1.getTsLength();i++)
    	{
    		chart.set_plot_data(channel1.getTs(i), channel1.getValue(i), i);
    	}
    	chart.plot_data();
    }
    
    // Variables declaration - do not modify                     
    private javax.swing.JButton exit_Button;
    private javax.swing.JTextArea raw_data_output;
    private javax.swing.JScrollPane scroll_pane;
    private javax.swing.JPanel jPanel1;
    private javax.swing.JTabbedPane jTabbedPane1;
    private javax.swing.JButton send_Button;
    private javax.swing.JLabel url_Label;
    private javax.swing.JTextField url_Textfield;
    // End of variables declaration       
    
    private Chart_Test chart;
    private ChannelREST channel1;
}
